package net.huashitong.Indicators.web;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.CompletionClassificationService;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.Indicators.utils.ComparatorUtils;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import net.huashitong.Indicators.utils.IndustryNameUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

/**
 * 首页
 *
 * @author lzr
 */
@Controller
@RequestMapping("/app/indicatorBreakdown/")
public class IndicatorBreakdownAppController extends JsonBaseController {

    @Autowired
    private IndicatorBreakdownService indicatorBreakdownService;
    @Autowired
    private CompletionClassificationService completionClassificationService;

    @Override
    protected String getView(String arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @ResponseBody
    @RequestMapping("/queryAll")
    public Message queryAll(@RequestBody IndicatorBreakdownFilter filter) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        //计算未达标
        List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list3 = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list4 = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list5 = new ArrayList<Map<String, Object>>();
        String judge = DataUtils.getValue(filter);
        String datese = DataUtils.getValueMonth(judge, filter);
        datese = DateForIndUtils.getMonth(datese);
        String[] industryNames = {
                "农村居民人均可支配收入", "地区生产总值", "第一产业增加值", "第二产业增加值", "第三产业增加值", "建筑业增加值", "社会消费品零售总额", "规模以上工业增加值", "固定资产投资", "工业固投", "一般公共预算总收入", "一般公共预算收入", "进出口总值", "实际利用外资"/*,"农林牧渔业总产值"*/, "城镇居民人均可支配收入",
        };
        for (int i = 0; i < industryNames.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            List breakdown = indicatorBreakdownService.queryAll(datese, industryNames[i], filter.getArea());
            if (ObjectUtils.isBlank(breakdown)) {
                continue;
            }
            Object[] object = (Object[]) breakdown.get(0);
            map.put("industryId", object[0]);
            Double double1 = Double.parseDouble(object[1].toString());
            Double double2 = Double.parseDouble(object[5].toString());
            BigDecimal data1 = new BigDecimal(double1);
            BigDecimal data2 = new BigDecimal(double2);
            int k = data1.compareTo(data2);
            if (k < 0) {
                map.put("reach", "0");
            } else {
                map.put("reach", "1");
            }
            map.put("ratio", object[1]);
            map.put("industryName", object[2]);
            map.put("cumulative", object[3]);
            map.put("rank", object[4].toString());
            if ("城镇居民人均可支配收入".equals(industryNames[i]) || "农村居民人均可支配收入".equals(industryNames[i])) {
                map.put("unit", "元");
            } else {
                map.put("unit", object[6]);
            }
            list.add(map);
        }

        Collections.sort(list, new ComparatorUtils());
        for (Map<String, Object> map : list) {
            map = changeName(map);
            if ("亿元".equals(map.get("unit"))) {
                String val = map.get("cumulative").toString();
                Double double1 = Double.parseDouble(val);
                double1 = double1 / 10000;
                String st = "";
                DecimalFormat df = new DecimalFormat("#.##");
                st = df.format(double1);
                map.put("cumulative", st);
            } else {
                String val = map.get("cumulative").toString();
                Double double1 = Double.parseDouble(val);
                String st = "";
                DecimalFormat df = new DecimalFormat("#.##");
                st = df.format(double1);
                map.put("cumulative", st);
            }
            Double double2 = Double.parseDouble(map.get("ratio").toString());
            DecimalFormat df = new DecimalFormat("#.##");
            String st = df.format(double2);
            map.put("ratio", st);
            if ("0".equals(map.get("reach"))) {
                list2.add(map);
            }
            if ("1".equals(map.get("rank")) || "2".equals(map.get("rank")) || "3".equals(map.get("rank"))) {
                list3.add(map);
            }
        }
        list4.add(list.get(0));
        list4.add(list.get(1));
        list4.add(list.get(2));
        String date = datese.substring(0, 4) + "年" + datese.substring(datese.length() - 2) + "月";
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("list", list3);
        maps.put("type", "1");
        maps.put("date", date);
        list5.add(maps);
        maps = new HashMap<String, Object>();
        maps.put("list", list4);
        maps.put("type", "2");
        maps.put("date", date);
        list5.add(maps);
        maps = new HashMap<String, Object>();
        maps.put("list", list2);
        maps.put("type", "3");
        maps.put("date", date);
        list5.add(maps);
        return coverMessage("200", "获取成功", list5);
    }


    public Map<String, Object> changeName(Map<String, Object> map) {

        if (map.get("industryName").toString().contains("农村")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "农村人均收入" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "农村人均收入");
            }

        }
        if (map.get("industryName").toString().contains("公共预算总收入")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "公共预算总收入" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "公共预算总收入");
            }

        }
        if (map.get("industryName").toString().contains("公共预算收入")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "公共预算收入" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "公共预算收入");
            }
        }
        if (map.get("industryName").toString().contains("第一产业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "第一产业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "第一产业");
            }
        }
        if (map.get("industryName").toString().contains("第二产业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "第二产业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "第二产业");
            }
        }
        if (map.get("industryName").toString().contains("第三产业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "第三产业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "第三产业");
            }
        }
        if (map.get("industryName").toString().contains("农林牧渔业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "农林牧渔业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "农林牧渔业");
            }
        }
        if (map.get("industryName").toString().contains("城镇")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "城镇人均收入" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "城镇人均收入");
            }

        }
        if (map.get("industryName").toString().contains("规模以上工业增加值")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "规模以上工业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "规模以上工业完成情况");
            }

        }
        if (map.get("industryName").toString().contains("建筑业")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "建筑业" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "建筑业");
            }
        }
        if (map.get("industryName").toString().contains("地区生产总值")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "GDP" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "GDP");
            }

        }
        if (map.get("industryName").toString().contains("社会消费品零售总")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "全社会消费品零售总额 " + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "全社会消费品零售总额 ");
            }
        }
        if (map.get("industryName").toString().contains("实际利用")) {
            if (map.get("industryName").toString().contains("季")) {
                map.put("industryName", "实际外资" + map.get("industryName").toString().substring(map.get("industryName").toString().length() - 6, map.get("industryName").toString().length()));
            } else {
                map.put("industryName", "实际外资");
            }

        }

        return map;
    }

    /**
     * 排名页接口
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryRank")
    public Message queryRank(@RequestBody IndicatorBreakdownFilter filter) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        String[] industryNames = {
                "地区生产总值", "第一产业增加值", "第二产业增加值", "第三产业增加值", "建筑业增加值", "社会消费品零售总额", "规模以上工业增加值", "固定资产投资", "工业固投", "一般公共预算总收入", "一般公共预算收入", "进出口总值", "实际利用外资"/*,"农林牧渔业总产值"*/, "城镇居民人均可支配收入", "农村居民人均可支配收入"
        };
        String d = filter.getDate();
        for (int i = 0; i < industryNames.length; i++) {
            filter.setDate(d);
            filter.setDate(DateForIndUtils.getMonth(filter.getDate()));
            Map<String, Object> map = new HashMap<String, Object>();
            List breakdown = indicatorBreakdownService.queryAll(filter.getDate(), industryNames[i], filter.getArea());
            String mData = "01";
            String siz = "0";
            if (ObjectUtils.isBlank(breakdown)) {
                if ("1".equals(filter.getState())) {
                    siz = "1";

                    filter.setIndustryName(industryNames[i]);
                    filter = IndustryNameUtils.changeId(filter);
                    String judge = DataUtils.getValue(filter);
                    String date = DataUtils.getValueMonth(judge, filter);
                    breakdown = indicatorBreakdownService.queryAll(DateForIndUtils.getMonth(date), industryNames[i], filter.getArea());
                    mData = date.substring(date.length() - 2, date.length());
                } else {
                    continue;
                }
            }
            Object[] object = (Object[]) breakdown.get(0);
            Double double1 = Double.parseDouble(StringUtils.isBlank(object[1].toString()) ? "0" : object[1].toString());
            Double double2 = Double.parseDouble(object[5].toString());
            BigDecimal data1 = new BigDecimal(double1);
            BigDecimal data2 = new BigDecimal(double2);
            int k = data1.compareTo(data2);
            if (k < 0) {
                map.put("reach", "0");
            } else {
                if ("1".equals(map.get("rank")) || "2".equals(map.get("rank")) || "3".equals(map.get("rank"))) {
                    map.put("reach", "2");
                } else {
                    map.put("reach", "1");
                }
            }

            map.put("industryId", object[0]);
            map.put("ratio", object[1]);
            if ("1".equals(siz) && "1".equals(filter.getState())) {
                if ("03".equals(mData)) {
                    map.put("industryName", object[2] + "(第一季度)");
                } else if ("06".equals(mData)) {
                    map.put("industryName", object[2] + "(第二季度)");
                } else if ("09".equals(mData)) {
                    map.put("industryName", object[2] + "(第三季度)");
                } else if ("12".equals(mData)) {
                    map.put("industryName", object[2] + "");
                }

            } else {
                map.put("industryName", object[2]);
            }
            map.put("cumulative", object[3]);

            if ("规模以上工业增加值".equals(industryNames[i]) && "201701-11".equals(filter.getDate())) {
                map.put("rank", "5");
            } else if ("规模以上工业增加值".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "11");
            }

            if ("社会消费品零售总额".equals(industryNames[i]) && "201701-12".equals(filter.getDate())) {
                map.put("rank", "5");
            } else if ("社会消费品零售总额".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "6");
            } else {
                map.put("rank", object[4] == null ? "-" : object[4].toString());
            }

            if ("第一产业增加值".equals(industryNames[i]) && "201701-12".equals(filter.getDate())) {
                map.put("rank", "1");
            }

            if ("城镇居民人均可支配收入".equals(industryNames[i]) || "农村居民人均可支配收入".equals(industryNames[i])) {
                map.put("unit", "元");
            } else {
                map.put("unit", object[6]);
            }
            if ("规模以上工业产值".equals(industryNames[i])) {
                map.put("rank", "-");
            }
			/* if("01".equals(mData)&&!"进出口总值".equals(industryNames[i])){
				 map.put("rank", "0");
			 }*/
            if ("固定资产投资".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("一般公共预算总收入".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("一般公共预算收入".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "4");
            }
            if ("规模以上工业增加值".equals(industryNames[i]) && "201801-02".equals(filter.getDate())) {
                map.put("rank", "11");
            }


            if ("固定资产投资".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "2");
            }
            if ("工业固投".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "9");
            }
            if ("实际利用外资".equals(industryNames[i]) && "201801-03".equals(filter.getDate())) {
                map.put("rank", "-");
            }
            if ("规模以上工业增加值".equals(industryNames[i]) && "201801-05".equals(filter.getDate())) {
                map.put("rank", "6");
            }
            list.add(map);
        }
        if (ObjectUtils.isBlank(list)) {
            return coverMessage("500", "暂无数据");
        }
        for (Map<String, Object> map : list) {
            map = changeName(map);
            if ("亿元".equals(map.get("unit"))) {
                String val = map.get("cumulative").toString();

                Double double1 = Double.parseDouble(val);
                double1 = double1 / 10000;
                String st = "";
                if (double1 < 1) {
                    map.put("cumulative", double1);
                } else {
                    DecimalFormat df = new DecimalFormat("#.##");
                    st = df.format(double1);
                    map.put("cumulative", st);
                }
            } else {
                String val = map.get("cumulative").toString();
                Double double1 = Double.parseDouble(val);
                String st = "";
                DecimalFormat df = new DecimalFormat("#.##");
                st = df.format(double1);
                map.put("cumulative", st);
            }
            Double double2 = Double.parseDouble(StringUtils.isBlank(map.get("ratio").toString()) ? "0" : map.get("ratio").toString());
            if (double2 > 1) {
                DecimalFormat df = new DecimalFormat("#.##");
                String st = df.format(double2);
                map.put("ratio", st);
            } else {
                DecimalFormat df = new DecimalFormat("#.##");
                String st = df.format(double2);
                map.put("ratio", st);
            }

        }
        return coverMessage("200", "数据获取成功", list);
    }

    /**
     * 返回有数据的月份
     *
     * @param filter
     * @return
     */
    @RequestMapping("/queryHaveValue")
    @ResponseBody
    public Message queryHaveValue(@RequestBody IndicatorBreakdownFilter filter) {
        String judge = DataUtils.getValue(filter);
        Map<String, Object> map = new HashMap<String, Object>();
        String date = DataUtils.getValueMonth(judge, filter);
        map.put("judge", judge);
        map.put("date", date);
        return coverMessage("200", "获取成功", map);
    }

    @RequestMapping("/queryIndustryNameList")
    @ResponseBody
    public Message queryIndustryNameList() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        map.put("title", "主要经济指标情况");
        map.put("title2", "重点项目情况");
        map.put("name", "项目总数：129项目");
        map.put("name2", "新开工项目：79项");
        map.put("name3", "在建项目：50项目");
        String[] industryNames = {
                "地区生产总值", "固定资产投资", "建筑业增加值", "社会消费品零售总额", "规模以上工业增加值", "一般公共预算收入"
        };
        DecimalFormat df = new DecimalFormat("#.##");
        for (int i = 0; i < industryNames.length; i++) {
            Map<String, Object> maps = new HashMap<String, Object>();
            IndicatorBreakdownFilter filter = new IndicatorBreakdownFilter();
            filter.setArea("永泰县");
            filter.setIndustryName(industryNames[i]);
            filter = IndustryNameUtils.changeId(filter);
            String judge = DataUtils.getValue(filter);
            String date = DataUtils.getValueMonth(judge, filter);
            List breakdown = indicatorBreakdownService.queryAll(DateForIndUtils.getMonth(date), industryNames[i], filter.getArea());
            Object[] object = (Object[]) breakdown.get(0);

            if ("地区生产总值".equals(industryNames[i])) {
                maps.put("targetCumulative", "184.1");
                maps.put("targetRatio", "11");
            } else if ("固定资产投资".equals(industryNames[i])) {
                maps.put("targetCumulative", "134");
                maps.put("targetRatio", "20");
            } else if ("建筑业增加值".equals(industryNames[i])) {
                maps.put("targetCumulative", "504");
                maps.put("targetRatio", "15");
            } else if ("社会消费品零售总额".equals(industryNames[i])) {
                maps.put("targetCumulative", "70");
                maps.put("targetRatio", "16.5");
            } else if ("规模以上工业增加值".equals(industryNames[i])) {
                maps.put("targetCumulative", "72.7");
                maps.put("targetRatio", "13");
            } else if ("一般公共预算收入".equals(industryNames[i])) {
                maps.put("targetCumulative", "10.5");
                maps.put("targetRatio", "15.4");
            }
            maps.put("industryId", object[0]);
            maps.put("cumulative", object[3]);
            maps.put("ratio", object[1]);
            //完成数
            String val = maps.get("cumulative").toString();
            Double double1 = Double.parseDouble(val);
            Double double3 = Double.parseDouble(maps.get("targetCumulative").toString());
            double1 = double1 / 10000;
            maps.put("cumulativeState", "0");
			/*if(double1>=double3){
				 maps.put("cumulativeState","0");
			}else if(double1<double3){
				 maps.put("cumulativeState","1");
			}*/
            String st = "";
            if (double1 < 1) {
                maps.put("cumulative", double1);
            } else {
                st = df.format(double1);
                maps.put("cumulative", st);
            }
            if ("规模以上工业增加值".equals(industryNames[i])) {
                maps.put("cumulative", "-");
            }
            Double double2 = Double.parseDouble(maps.get("ratio").toString());
            Double double4 = Double.parseDouble(maps.get("targetRatio").toString());
            if (double2 >= double4) {
                maps.put("ratioState", "0");
            } else if (double1 < double3) {
                maps.put("ratioState", "1");
            }
            if (double2 > 1) {
                st = df.format(double2);
                maps.put("ratio", st);
            } else {
                st = df.format(double2);
                maps.put("ratio", st);
            }

            if ("社会消费品零售总额".equals(industryNames[i])) {
                maps.put("industryNames", "全社会消费品零售总额");
            } else {
                maps.put("industryNames", industryNames[i]);
            }
            if ("一般公共预算收入".equals(industryNames[i])) {
                maps.put("industryNames", "一般公共预算收入");
            }
            maps.put("unit", object[6]);
            list.add(maps);
        }
        map.put("lists", list);
        return coverMessage("200", "获取成功", map);
    }

    @ResponseBody
    @RequestMapping("/updateWarningContents")
    public Message updateWarningContents(@RequestBody IndicatorBreakdownFilter filter) {
        CompletionClassification cf = completionClassificationService.getById(filter.getSysId());
        cf.setWarningContents(filter.getWarningContents());
        completionClassificationService.update(cf);
        return coverMessage("200", "更新预警内容成功");
    }

}
